﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QualityStormCommon;
using System.Data.Objects;

namespace QualityStorm {
    public partial class FrmRoles : Form {
        Role m_role = null;
        ConnectionContextBag m_contextBag;

        bool m_ReadOnly = false;

        public bool ReadOnly {
            get {
                return m_ReadOnly;
            }
            set {
                m_ReadOnly = value;
                SetReadOnly();
            }
        }

        private void SetReadOnly() {
            txtName.Enabled = !m_ReadOnly;
            grdPermissions.Enabled = !m_ReadOnly;
            btnOK.Enabled = !m_ReadOnly;
        }


        public ConnectionContextBag ContextBag {
            get {
                return m_contextBag;
            }
            set {
                m_contextBag = value;
            }
        }

        public Role Role {
            get {
                return m_role;
            }
            set {
                m_role = value;
                ShowData();
            }
        }

        private void ShowData() {
            if (m_role != null) {
                txtName.Text = m_role.Name;
            }
            List<ModulePermission> list = new List<ModulePermission>();



            List<DataGridViewRow> rows = new List<DataGridViewRow>();

            Hashtable map = new Hashtable();
            Module[] modules = m_contextBag.Context.Service.GetModules();
            ObjectPermission[] permissions =null;
            foreach (Module module in modules) {
                if (m_role != null) {
                    permissions = m_contextBag.Context.Service.GetObjectPermissions(module.Guid, m_role);
                }

                string moduleName = module.Name;
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewTextBoxCell moduleCell = new DataGridViewTextBoxCell();
                moduleCell.Value = moduleName;

                DataGridViewTriStateCell viewCell = new DataGridViewTriStateCell();
                DataGridViewTriStateCell modifyCell = new DataGridViewTriStateCell();
                DataGridViewTriStateCell createCell = new DataGridViewTriStateCell();
                DataGridViewTriStateCell deleteCell = new DataGridViewTriStateCell();

                viewCell.StateValue = Permission.None;
                modifyCell.StateValue = Permission.None;
                createCell.StateValue = Permission.None;
                deleteCell.StateValue = Permission.None;


                row.Cells.Add(moduleCell);
                row.Cells.Add(viewCell);
                row.Cells.Add(createCell);
                row.Cells.Add(modifyCell);
                row.Cells.Add(deleteCell);
                row.Tag = module;

                rows.Add(row);
                map.Add(module.Guid, row);

                if (permissions != null)
                    foreach (ObjectPermission permission in permissions) {
                        if (permission.ObjectGuid == module.Guid) {
                            try {
                                ((DataGridViewTriStateCell)row.Cells[1]).StateValue = (Permission)permission.View;
                                ((DataGridViewTriStateCell)row.Cells[2]).StateValue = (Permission)permission.Create;
                                ((DataGridViewTriStateCell)row.Cells[3]).StateValue = (Permission)permission.Modify;
                                ((DataGridViewTriStateCell)row.Cells[4]).StateValue = (Permission)permission.Delete;
                            } catch {
                            }
                        }
                    }

                grdPermissions.Rows.Add(row);


            }
        }

        public FrmRoles(ConnectionContextBag bag) {
            InitializeComponent();
            this.ContextBag = bag;
        }
        public FrmRoles() {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (txtName.Text == "") {
                MessageBox.Show("Please inform the name of the Role.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            try {
                if (m_role == null) {
                    m_role = Role.CreateRole(Guid.NewGuid());
                    m_role.Name = txtName.Text;
                    if (m_contextBag.Folder != null) {
                        m_role.Parent = m_contextBag.Folder.Guid;
                    } else {
                        m_role.Parent = m_contextBag.Module.Guid;
                    }
                    m_contextBag.Context.Service.AddRole(m_role);
                    m_contextBag.Context.Service.SaveChanges();
                    
                } else {
                    m_role.Name = txtName.Text;
                    m_contextBag.Context.Service.UpdateRole(m_role);
                    m_contextBag.Context.Service.SaveChanges();
                    
                }
                
                
                // Saving the permissions
                foreach (DataGridViewRow row in this.grdPermissions.Rows) {
                    m_contextBag.Context.Service.SetObjectPermission(
                        ((Module)row.Tag).Guid,
                        m_role,
                        ((DataGridViewTriStateCell)row.Cells["View"]).StateValue,
                        ((DataGridViewTriStateCell)row.Cells["Create"]).StateValue,
                        ((DataGridViewTriStateCell)row.Cells["Modify"]).StateValue,
                        ((DataGridViewTriStateCell)row.Cells["Delete"]).StateValue);


                    
                }
                this.DialogResult = DialogResult.OK;
                Close();

                
            } catch (ObjectAlreadyCreatedException oace) {
                MessageBox.Show("There is already a Role with this name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void FrmRoles_Load(object sender, EventArgs e) {
            List<ModulePermission> permissions = new List<ModulePermission>();
            Module[] modules = m_contextBag.Context.Service.GetModules();



        }
    }

    class ModulePermission {
        private Module m_Module;
        private Permission m_View;
        private Permission m_Create;
        private Permission m_Modify;
        private Permission m_Delete;

        public ModulePermission(Module module,
            Permission view,
            Permission create,
            Permission modify,
            Permission delete) {

            this.m_Module = module;
            this.m_Create = create;
            this.m_Modify = modify;
            this.m_Delete = delete;
            this.m_View = view;

        }

        public string Module {
            get {
                return m_Module.Name;
            }
        }

        public Module GetModule() {
            return m_Module;
        }

        public Permission View {
            get {
                return m_View;
            }
            set {
                m_View = value;
            }
        }

        public Permission Create {
            get {
                return m_Create;
            }
            set {
                m_Create = value;
            }
        }

        public Permission Modify {
            get {
                return m_Modify;
            }
            set {
                m_Modify = value;
            }
        }

        public Permission Delete {
            get {
                return m_Delete;
            }
            set {
                m_Delete = value;
            }
        }


    }
}
